﻿using System;
using System.Collections;
using System.Text;
using Portal.Core;
using Portal.Core.Utils;
using Portal.Data;
using Portal.Data.Entity;
using Portal.Data.Helper;

namespace Ajax.UserControl
{
    public partial class Ajax_UserControl_Comment : System.Web.UI.UserControl
    {
        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            var action = Request["act"];
            var email = Request.Form["email"];
            switch (action)
            {
                case "new":
                    if (string.IsNullOrEmpty(Request.Form["fullname"]) || string.IsNullOrEmpty(email)
                       || string.IsNullOrEmpty(Request.Form["title"]) || string.IsNullOrEmpty(Request.Form["body"])
                       || string.IsNullOrEmpty(Request.Form["cid"])
                        )
                        writer.Write("Có lỗi xảy ra do nhập thiếu dữ liệu!");
                    else if (!Mail.IsEmail(email))
                    {
                        writer.Write("Sai định dạng email!");
                    }
                    else
                    {
                        var cm = new Comment
                        {
                            ContentId = Convert.ToInt32(Request.Form["cid"]),
                            Body = Request.Form["body"],
                            Title = Request.Form["title"],
                            Address = Request.Form["address"],
                            FullName = Request.Form["fullname"],
                            Email = email,
                            SentDate = DateTime.Now
                        };
                        ORMHelper.InstanceIgnorePermission.Save(cm);
                        if (cm.ID > 0)
                        {
                            Comment.RecachingCounter(cm.ContentId, ObjectStatusClass.GetByName(cm.Status.ToString()));
                            Comment.RecachingCounter(cm.ContentId, null);
                        }

                        writer.Write("Cảm ơn bạn đã đóng góp ý kiến về bài viết!");
                        Response.End();
                    }
                    break;
                case "list":
                    //params:[cid=content id] [p=paging index;default=1]

                    var p = 0;

                    if (Request.QueryString["cid"] == null || !MyCheck.IsInt(Request.QueryString["cid"]))
                        return;
                    if (Request.QueryString["page"] != null && MyCheck.IsInt(Request.QueryString["page"]))
                        p = Convert.ToInt32(Request.QueryString["p"]);

                    var cid = Convert.ToInt32(Request.QueryString["cid"]);
                    var sb = new StringBuilder();
                    var count = 0;

                    //comments
                    var searchObj = new CommentSearch { ContentId = cid, IsCorrectStatus = true, Status = ObjectStatusClass.CommonStatus.Active };

                    IList cms = Comment.Search(searchObj, 10, p, out count);
                    if (cms.Count > 0)
                    {
                        sb.Append("<div class='comment-list'>");
                        foreach (Comment cm in cms)
                        {
                            sb.Append(
                                string.Format(
                                    "<div class='cominfo'><div class='comauthor'>{0}</div><div class='cmtime'>{1}</div></div>",
                                    cm.FullName, cm.Email)); //Process.GetDisplayTimer(cm.SentDate))
                            sb.Append(string.Format("<div class='comtitle'>{0}</div>", cm.Title));
                            sb.Append(string.Format("<div class='comcontent'>{0}</div>", cm.Body));
                            sb.Append("<br class='clearfix' />");
                        }
                        sb.Append("</div'>");
                    }
                    writer.Write(sb.ToString());
                    break;
            }
        }
    }
}